#!/bin/bash

target="/home/aqdms/CARBLog/sys_resources/carbput.sh"
temp="/home/aqdms/CARBLog/sys_resources/temp"
date_stamp=$(date +%F)
U_LOG="/home/aqdms/CARBLogger_upgrade_log"
backup="$target.bak-$date_stamp"
check_ver=$(grep "VERSION 2" < $target | wc -l)

if [ "$check_ver" -eq 0 ]; then
	cp $target $backup
	sed -e '/THIS SCRIPT CONNECTS/ i\#VERSION 2' \
		-e '/THIS SCRIPT CONNECTS/ i\#edited by MQ 3/13/2013' \
		-e '/PASSWD\=/ i\FTPLOG="ftplog"' \
		-e '/PASSWD\=/ i\SUCCESS="226 File receive OK."' \
		-e 's:ftp -niv:timeout 60 ftp -niv:' \
		-e 's:EOF 1> /dev/null 2> /dev/null:EOF 1> $FTPLOG:' \
		-e '/rm -f/ i\if fgrep "$SUCCESS" $FTPLOG; then' \
		-e 's:rm -f $1 1> /dev/null 2> /dev/null:	rm -f $1 1> /dev/null 2> /dev/null\
fi:' <$target >$temp
	mv $temp $target
	echo "$date_stamp :: Older version of carbput.sh found - ver. 2 changes applied. back up file $backup created" >> $U_LOG	
else
	echo "$date_stamp :: Ver. 2 of carbput.sh already in place, no changes applied" >> $U_LOG
fi